package cn.ds.assets.module.assets.service.metadataJobInstance;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.time.LocalDateTime;

import cn.ds.assets.module.assets.controller.admin.metadataJobInstance.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.metadataJobInstance.BaseMetadataJobInstanceDO;
import cn.ds.assets.framework.common.pojo.PageResult;

import cn.ds.assets.module.assets.convert.metadataJobInstance.BaseMetadataJobInstanceConvert;
import cn.ds.assets.module.assets.dal.mysql.metadataJobInstance.BaseMetadataJobInstanceMapper;

import static cn.ds.assets.framework.common.exception.util.ServiceExceptionUtil.exception;
import cn.ds.assets.framework.common.exception.enums.GlobalErrorCodeConstants;
//import static cn.ds.assets.module.assets.enums.ErrorCodeConstants.*;

/**
 * 元数据管理-元数据采集作业实例 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class BaseMetadataJobInstanceServiceImpl implements BaseMetadataJobInstanceService {

    @Resource
    private BaseMetadataJobInstanceMapper baseMetadataJobInstanceMapper;

    @Override
    public Long createBaseMetadataJobInstance(BaseMetadataJobInstanceCreateReqVO createReqVO) {
        // 插入
        BaseMetadataJobInstanceDO baseMetadataJobInstance = BaseMetadataJobInstanceConvert.INSTANCE.convert(createReqVO);
        baseMetadataJobInstanceMapper.insert(baseMetadataJobInstance);
        // 返回
        return baseMetadataJobInstance.getId();
    }

    @Override
    public void updateBaseMetadataJobInstance(BaseMetadataJobInstanceUpdateReqVO updateReqVO) {
        // 校验存在
        validateBaseMetadataJobInstanceExists(updateReqVO.getId());
        // 更新
        BaseMetadataJobInstanceDO updateObj = BaseMetadataJobInstanceConvert.INSTANCE.convert(updateReqVO);
        baseMetadataJobInstanceMapper.updateById(updateObj);
    }

    @Override
    public void deleteBaseMetadataJobInstance(Long id) {
        // 校验是否存在
        validateBaseMetadataJobInstanceExists(id);
        // 逻辑删除
        baseMetadataJobInstanceMapper.updateToDelete(id, LocalDateTime.now());
    }

    private void validateBaseMetadataJobInstanceExists(Long id) {
        if (id == null) {
            throw exception(GlobalErrorCodeConstants.ID_NOT_FOUND);
        }
        BaseMetadataJobInstanceDO dataDo = baseMetadataJobInstanceMapper.selectById(id);
        if (dataDo == null) {
            throw exception(GlobalErrorCodeConstants.DATA_NOT_FOUND);
        }
    }

    @Override
    public BaseMetadataJobInstanceDO getBaseMetadataJobInstance(Long id) {
        return baseMetadataJobInstanceMapper.selectById(id);
    }

    @Override
    public List<BaseMetadataJobInstanceDO> getBaseMetadataJobInstanceList(Collection<Long> ids) {
        return baseMetadataJobInstanceMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BaseMetadataJobInstanceDO> getBaseMetadataJobInstancePage(BaseMetadataJobInstancePageReqVO pageReqVO) {
        return baseMetadataJobInstanceMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BaseMetadataJobInstanceDO> getBaseMetadataJobInstanceList(BaseMetadataJobInstanceExportReqVO exportReqVO) {
        return baseMetadataJobInstanceMapper.selectList(exportReqVO);
    }

}
